package com.xyw.his00.utils.NumberUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.Map;

public class TokenUtil {
    private static String secretKEY="xIaoMai12*-2";//小埋天下无双
    public static String getToken(Map<String,Object> map){
        Calendar d=Calendar.getInstance();
        d.add(Calendar.DATE,10);
    String token=Jwts.builder().setClaims(map)
            .setExpiration(d.getTime())
            .claim("userName",map.get("realName"))
            .signWith(SignatureAlgorithm.HS256,secretKEY)
            .compact();
   return token;
    }
    public static Claims verify(String token) {
         //todo:这里为什么字节数组和字符串参数不能合并谈论
       SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; // 确保算法与生成JWT时相同
        //将密钥转换为字节数组进行解码使用
        byte[] serectKeysBytes=secretKEY.getBytes(StandardCharsets.UTF_8);
        try {
            //解析JWT并获取Claims对象
            Claims claims= Jwts.parser()
                    .setSigningKey(secretKEY)
                    .parseClaimsJws(token)
                    .getBody();
            return claims;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }
    }
}
